<%@ page pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>登录</title>
<!-- ExtJS -->
<link rel="stylesheet" type="text/css"
	href="<%=request.getContextPath()%>/public/extjs/resources/css/ext-all.css" />
<link href="<%=request.getContextPath()%>/public/style/default/css/login.css" rel="stylesheet" type="text/css">
<script type="text/javascript"
	src="<%=request.getContextPath()%>/public/extjs/ext-all.js"></script><script type="text/javascript"	src="<%=request.getContextPath()%>/public/include/js/component.js"></script><script type="text/javascript" src="<%=request.getContextPath()%>/public/include/js/api/appdeal.js"></script>


<!-- GC -->
<%
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
%>


</head>
<body >


</body>
<script language="javascript">
	 var html = "";
	 /**
	Ext.Ajax.request({
		url : "common.do?action=getDeptInfoByDeptRole",
		params : {
			deptRoleId : 'SYSRIGHTDEPT'
		},
		method : "POST",
		success : function(response) {
			var list = Ext.JSON.decode(response.responseText);
			for(var i=0;i<list.length;i++){
				var obj = list[i];
				if(i==0) html = "<option value="+obj.uuid+" selected>"+obj.deptName+"</option>";
				else html += "<option value="+obj.uuid+">"+obj.deptName+"</option>";
			}
			document.getElementsByName("Department")[0].innerHTML = html;
		},
		failure : function() {
			//Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
		}
	}); 
	**/
	function isOsSubmit(){
		return navigator.userAgent.indexOf("Firefox")>0;
	}
	//alert(navigator.userAgent.indexOf("Safari")>0);
	function setWinLocation(){
		var bodywidth = document.body.clientWidth;
		var bodyheight = document.body.clientHeight;
		var top1 = document.getElementById("loginmain").offsetTop;
		var left1 = document.getElementById("loginmain").offsetLeft;
		var lwidth = document.getElementById("loginmain").offsetWidth;
		var lheight = document.getElementById("loginmain").offsetHeight;
		top1 = (bodyheight-lheight)/2;
		left1 = (bodywidth-lwidth)/2;
		document.getElementById("loginmain").style.top = top1;
		document.getElementById("loginmain").style.left = left1;
	}
	
	function checkKey(event){
		var explorer = window.navigator.userAgent ;
		event = event || window.event;
		var keycode = event.keyCode||event.which||event.charCode;
		//ie 
		/* if (explorer.indexOf("MSIE") >= 0) {
			keycode = event.keyCode;
		}
		//firefox 
		else if (explorer.indexOf("Firefox") >= 0) {
			
			keycode = event.keyCode||event.which||event.charCode;
		}
		//Chrome
		else if(explorer.indexOf("Chrome") >= 0){
			keycode = event.keyCode;
		}else{
			
		} */
		if(keycode == 13){
			f_login();
		}
	}
	
	function f_login(){
		
		var loginname = encodeURIComponent(document.getElementById("txtname").value);
		if(loginname == null || loginname == ""){
			alert("请输入用户名！");
			return;
		}
		var password = document.getElementById("password").value;
		if(password == null || password == ""){
			alert("请输入密码！");
			return;
		}
		// Modify by zonghuah at 2016-01-21 登录名是中文处理
		loginname = encodeURIComponent(loginname);
		
		//var departmentId = document.forms[0].Department.value;	
		var host=window.location.host;
		
		var url = '<%=request.getContextPath()%>/sysright_login.do?loginname=' + loginname + '&password=' + password + '&time=' + (new Date()).getTime() ;//+ '&departmentId='+departmentId
		
		var xhr = null;
		if(window.XMLHttpRequest){
			xhr = new XMLHttpRequest();
		}else{
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xhr.open("GET",url,true);
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4){
				if(xhr.status == 200){
					var res = eval('('+xhr.responseText+')');
					if(res.errorType != null && res.errorType != undefined){
						alert(res.msg);
						return;
					}
					
					window.location.href = 'mainFrame.do?action=showOpenMainFrame';
					
				}
			}
		}
		xhr.send(null);
	}

	window.onload=function(){
	 	
		//document.forms[0].password.focus();
		//document.forms[0].txtname.value=document.forms[0].username.value;
		//setWinLocation();
	}

	 Ext.require([ 'Ext.form.*', 'Ext.layout.container.Absolute',
			'Ext.window.Window' ]);

	Ext.onReady(function() {
		var form = Ext.create('Ext.form.Panel', {
			layout : 'absolute',
			url : 'save-form.php',
			defaultType : 'textfield',
			id : 'loginForm',
			border : false,

			items : [ {
				fieldLabel : '用户名',
				fieldWidth : 30,
				msgTarget : 'side',
				allowBlank : false,
				x : 5,
				y : 5,
				name : 'loginname',
				id : 'loginname',
				anchor : '-5', // anchor width by percentage
				listeners : {
					specialkey : function(field, e) {
						if (e.getKey() == Ext.EventObject.ENTER) {
							Ext.getCmp('password').focus();
						}
					}
				}
			}, {
				fieldLabel : '密码',
				fieldWidth : 30,
				x : 5,
				y : 35,
				name : 'password',
				id : 'password',
				inputType : 'password',
				allowBlank : false,
				anchor : '-5', // anchor width by percentage
				listeners : {
					specialkey : function(field, e) {
						if (e.getKey() == Ext.EventObject.ENTER) {
							login();
						}
					}
				}
			} ]
		});

		var win = Ext.create('Ext.window.Window', {
			title : '登录',
			width : 300,
			id : 'loginWin',
			height : 125,
			minWidth : 200,
			minHeight : 120,
			layout : 'fit',
			//x : 700,
			//y : 300,
			plain : true,
			items : form,

			buttons : [ {
				text : '登录',
				handler : login
			}, {
				text : '放弃',
				handler : cancel
			} ]
		});

		win.show();
		/**
		 * 提交登陆请求
		 */
		function login() {
			var loginname = encodeURIComponent(Ext.getCmp('loginname').value);
			var password = Ext.getCmp('password').value;
			Ext.Ajax.request({
				url : '<%=request.getContextPath()%>/foa/login.do' ,
				waitTitle : "提示信息",
				method : 'POST',
				params : {
					   loginName : loginname,
					   password : password
					  },
				waitMsg : '正在验证您的身份,请稍候.....',
				success : function(form, action) {
						window.location.href = '<%=request.getContextPath()%>/foa/showOpenMainFrame.do';
							
				},
				failure : function(form, action) {
					var errmsg = action.result.msg;
					var errtype = action.result.errorType;
					Ext.Msg.alert("提示信息", errmsg, function() {
						var account = Ext.getCmp('loginname');
						var password = Ext.getCmp('password');
						if (errtype == '1') {
							//Ext.getCmp('loginForm').form.reset();
							account.setValue('');
							account.focus();
							account.validate();
						} else if (errtype == '2') {
							password.focus();
							password.setValue('');
						} else if (errtype == '3') {
							account.focus();
						}else {
							account.focus();
						}
					});
				}
			});
		}

		/**
		 * 放弃登陆请求
		 */
		function cancel() {
			window.close();
			self.close();
		}
	});
	
	function setWinLocation(){
		var bodywidth = document.body.clientWidth;
		var bodyheight = document.body.clientHeight;
		var bw = bodywidth/2;
		var bh = bodyheight/2;
		//alert(bodywidth+"           "+bodyheight);
		Ext.getCmp('loginWin').setPosition(bw,bh-50);
	} 
</script>
</html>